package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hqyj.exception.R;
import com.hqyj.pojo.Brand;
import com.hqyj.dao.BrandMapper;
import com.hqyj.service.BrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rock
 * @since 2023-03-06 02:50:09
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {

    @Resource
    BrandMapper brandMapper;
    @Autowired
    RedisTemplate<String,Object> redisTemplate;
    @Override
    public R queryBrandByType(Integer goodsTypeId) {
        String key = "goodsTypeId:"+goodsTypeId;
        List<Brand> list = null;
        Object obj  =redisTemplate.opsForList().range(key,0,-1);
        List<Object> listId =( List<Object> )obj;
        if(listId.size()==0){
            QueryWrapper<Brand> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("goods_type_id",goodsTypeId);
            list = brandMapper.selectList(queryWrapper);
            //存入缓存
            redisTemplate.opsForList().leftPushAll(key,list);
            // 设置时长
            redisTemplate.opsForList().getOperations().expire(key,15, TimeUnit.MINUTES);
        }else{
            list =( List<Brand> ) listId.get(0);
        }

        return R.ok("success",list);
    }
}
